﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using MediaMonitor.Common;

namespace MediaMonitor.Admin
{
    public partial class ChangeEmailsForNewCommercial : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillMediaTypes();
                BindData(1);
            }
        }
        protected void btnChangeEmail_Click(object sender, ImageClickEventArgs e)

        {
            try
            {
                SPs.EmailAddressesForNewCommercialAddUpdate(Convert.ToInt32(hdnID.Value.ToString()), txtEmailID1.Text.Trim(), txtEmailID2.Text.Trim(), txtEmailID3.Text.Trim(), txtEmailID4.Text.Trim(), txtEmailID5.Text.Trim(), Utility.ToInt(lstMediaType.SelectedValue)).ExecuteScalar();
                divsuccess.Visible = true;
                divError.Visible = false;
            }
            catch (Exception ex)
            {
                divsuccess.Visible = false;
                divError.Visible = true;
                throw;
            }
           
        }
        private void FillMediaTypes()
        {

            lstMediaType.DataSource = GetLookupDataSet().Tables["MediaType"];
            lstMediaType.DataBind();

            Utility.SelectListItemByText(lstMediaType, "TV");
            // lstMediaType.Items.Insert(0, new ListItem("- Select -", "Select"));
            // FillMedia(Utility.ToInt(lstMediaType.SelectedValue));

        }
        protected void lstMediaType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData(Utility.ToInt(lstMediaType.SelectedValue));
        }
        private static DataSet GetLookupDataSet()
        {

            //if (HttpContext.Current.Cache["dsLookUpByUser_TVCR_" + AppUser.CurrUserID.ToString()] == null)
            //{
            //Get Lookup Tables filtered by User / Company settings

            //DataSet ds = SPs.SpGetReportFilterLookupTables(AppUser.CurrUserID).GetDataSet();
            DataSet ds = SPs.GetLookupTables(AppUser.CurrUserID, "TVCR").GetDataSet();
            ds.Tables[0].TableName = "MediaType";
            ds.Tables[1].TableName = "Media";

            ds.Tables[3].TableName = "Advertiser";
            ds.Tables[4].TableName = "Brand";
            ds.Tables[5].TableName = "Sector";
            ds.Tables[6].TableName = "SubSector";
            ds.Tables[7].TableName = "Category";
            ds.Tables[8].TableName = "Product";
            ds.Tables[9].TableName = "ProdVersion";

            //HttpContext.Current.Cache.Insert("dsLookUpByUser_TVCR_" + AppUser.CurrUserID.ToString(), ds, null, DateTime.Now.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);


            return ds;

            //}
            //else
            //{
            // return ((DataSet)(HttpContext.Current.Cache["dsLookUpByUser_TVCR_" + AppUser.CurrUserID.ToString()]));
            //}
        }
       private void BindData(int mediaType)
       {
           DataTable dt = SPs.EmailAddressesForNewCommercial(mediaType).GetDataSet().Tables[0];
                if (dt.Rows.Count > 0)
                {
                    hdnID.Value = dt.Rows[0]["ID"].ToString();
                    txtEmailID1.Text = dt.Rows[0]["EmailID1"].ToString();
                    txtEmailID2.Text = dt.Rows[0]["EmailID2"].ToString();
                    txtEmailID3.Text = dt.Rows[0]["EmailID3"].ToString();
                    txtEmailID4.Text = dt.Rows[0]["EmailID4"].ToString();
                    txtEmailID5.Text = dt.Rows[0]["EmailID5"].ToString();
                }
                else
                {
                    hdnID.Value = "0";
                    txtEmailID1.Text = string.Empty;
                    txtEmailID2.Text = string.Empty;
                    txtEmailID3.Text = string.Empty;
                    txtEmailID4.Text = string.Empty;
                    txtEmailID5.Text = string.Empty;
                }
       }
    }
}